package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.KdBasicData;
import com.qijian.maindata.domain.query.KdBasicDataQuery;
import com.qijian.maindata.service.IKdBasicDataService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础资料Controller
 *
 * @author qijian
 * @date 2024-08-05
 */
@Api(tags = "金蝶基础资料")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/kdBasicData")
public class KdBasicDataController extends BaseController {

    private final IKdBasicDataService kdBasicDataService;

    /**
     * 查询基础资料列表
     */
    @GetMapping("/list")
    public TableDataInfo list(KdBasicDataQuery query, Page<KdBasicData> page) {
        initOrderItem(KdBasicData.class, page.orders());
        return getDataTable(kdBasicDataService.page(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 导出基础资料列表
     */
    @Log(title = "基础资料" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(KdBasicDataQuery query, HttpServletResponse response) {
        new ExcelUtil<>(KdBasicData. class).exportExcel(response,kdBasicDataService.list( WhereEntityTool.invoke(query)), "基础资料数据");
    }

    /**
     * 获取基础资料详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(kdBasicDataService.getById(id));
    }

    /**
     * 新增基础资料
     */
    @Log(title = "基础资料" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KdBasicData kdBasicData) {
        return toAjax(kdBasicDataService.save(kdBasicData));
    }

    /**
     * 修改基础资料
     */
    @Log(title = "基础资料" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KdBasicData kdBasicData) {
        approval(kdBasicDataService.getById(kdBasicData.getId()));
        return toAjax(kdBasicDataService.updateById(kdBasicData));
    }

    /**
     * 删除基础资料
     */
    @Log(title = "基础资料" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        ids.forEach(id -> approval(kdBasicDataService.getById(id)));
        return toAjax(kdBasicDataService.removeByIds(ids));
    }
}
